/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.MaintainCheckType;
import org.springblade.modules.admin.entity.MaintainPart;
import org.springblade.modules.admin.service.IMaintainCheckTypeService;
import org.springblade.modules.admin.service.IMaintainPartService;
import org.springblade.modules.admin.vo.MaintainPartVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 维保部件 控制器
 *
 * @author BladeX
 * @since 2021-05-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/maintainPart")
@Api(value = "维保部件", tags = "维保部件接口")
public class MaintainPartController extends BladeController {

	private final IMaintainPartService maintainPartService;

	private final IMaintainCheckTypeService maintainCheckTypeService;


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入maintainPart")
	public R<MaintainPart> detail(MaintainPart maintainPart) {
		MaintainPart detail = maintainPartService.getOne(Condition.getQueryWrapper(maintainPart));
		return R.data(detail);
	}

	/**
	 * 分页 维保部件
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入maintainPart")
	public R<IPage<MaintainPart>> list(MaintainPart maintainPart, Query query) {
		IPage<MaintainPart> pages = maintainPartService.page(Condition.getPage(query), Condition.getQueryWrapper(maintainPart));
		return R.data(pages);
	}

	/**
	 * 自定义分页 维保部件
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入maintainPart")
	public R<IPage<MaintainPartVO>> page(MaintainPartVO maintainPart, Query query) {
		IPage<MaintainPartVO> pages = maintainPartService.selectMaintainPartPage(Condition.getPage(query), maintainPart);
		return R.data(pages);
	}

	/**
	 * 新增 维保部件
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入maintainPart")
	public R save(@Valid @RequestBody MaintainPart maintainPart) {
		return R.status(maintainPartService.save(maintainPart));
	}

	/**
	 * 修改 维保部件
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入maintainPart")
	public R update(@Valid @RequestBody MaintainPart maintainPart) {
		return R.status(maintainPartService.updateById(maintainPart));
	}

	/**
	 * 新增或修改 维保部件
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入maintainPart")
	public R submit(@Valid @RequestBody MaintainPart maintainPart) {
		return R.status(maintainPartService.saveOrUpdate(maintainPart));
	}


	/**
	 * 删除 维保部件
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(maintainPartService.removeByIds(Func.toLongList(ids)));
	}

	@PostMapping("/init")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "数据初始化", notes = "")
	public R init(MultipartFile file) throws IOException {

		ExcelReader excelReader = ExcelUtil.getReader(file.getInputStream());
		List<List<Object>> lists = excelReader.read();
		List<MaintainPart> saveList = new ArrayList<>(16);
		List<String> names = new ArrayList<>(20);
		for (int i = 0; i < lists.size(); i++) {
			List<Object> objects = lists.get(i);
			String name1 = Convert.toStr(objects.get(1));
			String name2 = Convert.toStr(objects.get(2));
			MaintainCheckType maintainCheckType = maintainCheckTypeService.getOne(Wrappers.<MaintainCheckType>lambdaQuery().eq(MaintainCheckType::getName, name1));
			if (maintainCheckType == null) {
				names.add("一级="+name1+"----二级="+name2+"===行号=="+i);
				continue;
			}
			MaintainPart mp = new MaintainPart();
			mp.setName(name2);
			mp.setLocationId(maintainCheckType.getId());
			mp.setSort(i);
			saveList.add(mp);
		}
		;
		maintainPartService.saveBatch(saveList);
		return R.data(names);
	}
}
